﻿using UnityEngine;
using System.Collections;
using System;
using System.IO;

public class GameUtils {

	public static int GetTimeStamp(DateTime time)
	{
		DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
		return (int)(time - startTime).TotalSeconds;
	}
	public static int GetUTCTimeStamp()
	{
		return (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
	}
	/// <summary>
	/// Saves the texture as PNG format.
	/// </summary>
	/// <returns><c>true</c>, if texture as PN was saved, <c>false</c> otherwise.</returns>
	/// <param name="tex">Tex.</param>
	/// <param name="path">Path.</param>
	/// <param name="isOverWrite">If set to <c>true</c> is over write.</param>
	public static bool SaveTextureAsPNG(Texture2D tex, string path, bool isOverWrite=true)
	{
		if (!isOverWrite && File.Exists (path)) {
			Debug.LogWarning (path+" is already exit");
			return false;
		}
		if (tex == null) {
			Debug.LogWarning ("texture is null.");
			return false;
		}

		byte[] texData = tex.EncodeToPNG ();

		int index = path.LastIndexOf ('/');
		string dir = path.Remove (index);
		if (!Directory.Exists (dir)) {
			Directory.CreateDirectory (dir);
		}

		File.WriteAllBytes (path, texData);
		return true;
	}
}
